﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using Hopthoai;
using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.BUS.Security;
using GlacialComponents.Controls;
using QUANLYCHAMCONG.BL.Entities.Security;
namespace QUANLYCHAMCONG.PL.CMS.Hethong
{
    public partial class Phanquyen : UserControl
    {
        public delegate void _Close();
        public event _Close Close;
        Boolean bChildTrigger = true;
        Boolean bParentTrigger = true;
        int flag = 0;
        public Phanquyen()
        {
            InitializeComponent();
        }
        void LoadGroup()
        {
            DataTable ds = GROUPBUS.SelectAll();
            this.cmbgroup.DataSource=ds;
            this.cmbgroup.DisplayMember = "GROUPNAME";
            this.cmbgroup.ValueMember = "GROUPID";
        }
        void LoadALLLink()
        {
            DataTable dt = MENUBUS.SelectAll();
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    if (int.Parse(dr["MENUCHA"].ToString()) ==0 )
                    {
                        TreeNode tn = new TreeNode(dr["MENUNAME"].ToString());
                        tn.ImageIndex = 1;
                        tn.Name = dr["MENUID"].ToString();
                        //su ly add node con cua chuong trinh 
                        Authencation au = new Authencation();
                         au.VALUES = dr["MENUVALUE"].ToString();
                         dt = MENUGROUPBUS.SelectFlowForm(au);
                         foreach (DataRow row in dt.Rows)
                         {
                             TreeNode nodecon = new TreeNode(row["MENUNAME"].ToString());
                             nodecon.ImageIndex = 1;
                             nodecon.Name = row["MENUID"].ToString();
                             tn.Nodes.Add(nodecon);
                         }
                        tvdanhsachtinhnang.Nodes.Add(tn);
                    }
                }
            }
        }
        private void Phanquyen_Load(object sender, EventArgs e)
        {
            SetSave();
            LoadALLLink();
            LoadGroup();
            EnalbePanel(false);
        }
        void EnalbePanel(bool End)
        {
            if (End)
            {
                panel2.Enabled = true;
            }
            if (!End)
            {
                panel2.Enabled = false; 
            }
        }
        private void tvdanhsachtinhnang_AfterSelect(object sender, TreeViewEventArgs e)
        {
        }
        private void toolbar1_Add()
        {
            flag = 1;
            this.toolbar1.SetToolBar(false, false, false, true, true, false, true, false);
            panel2.Enabled = true;
            UnCheckALL();
        }
        void CheckAllChildren(TreeNode tn, Boolean bCheck)
        {
            bParentTrigger = false;
            foreach (TreeNode ctn in tn.Nodes)
            {
                bChildTrigger = false;
                ctn.Checked = bCheck;
                bChildTrigger = true;
                CheckAllChildren(ctn, bCheck);
            }
            bParentTrigger = true;
        }
        void CheckMyParent(TreeNode tn, Boolean bCheck)
        {
            if (tn == null) 
                return;
            if (tn.Parent == null)
                return;
            bChildTrigger = false;
            bParentTrigger = false;
            tn.Parent.Checked = bCheck;
            CheckMyParent(tn.Parent, bCheck);
            bParentTrigger = true;
            bChildTrigger = true;
        }
        private void tvdanhsachtinhnang_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (flag == 1)
            {
                if (bChildTrigger)
                {
                    CheckAllChildren(e.Node, e.Node.Checked);
                }
                if (bParentTrigger)
                {
                    CheckMyParent(e.Node, e.Node.Checked);
                }
            }
        }
        private void SetSave()
        {
            this.toolbar1.SetSave();
        }
        private void UnCheckALL()
        {
            foreach (TreeNode tn in tvdanhsachtinhnang.Nodes)
            {
                if (tn.Checked == true)
                    tn.Checked = false;
                foreach (TreeNode nodes2 in tn.Nodes)
                {
                    if (nodes2.Checked == true)
                        nodes2.Checked = false;
                }
            }
        }
        private void SetActive()
        {
            this.toolbar1.SetToolBar(true, true, true, false, true, false, false, false);
        }
        private void toolbar1_Save()
        {
            if (flag == 1)
            {
                if (this.cmbgroup.SelectedIndex != -1)
                {
                    //save all check in tree view ;
                    MENUGROUPCollection dsmenu = new MENUGROUPCollection();
                    foreach (TreeNode tn in tvdanhsachtinhnang.Nodes)
                    {
                        //luu du lieu nodes cha
                        if (tn.Checked == true)
                        {
                            MENUGROUP group = new MENUGROUP();
                            group.GROUPID = this.cmbgroup.SelectedValue.ToString();
                            group.MENUID = tn.Name.ToString();
                            dsmenu.Add(group);
                            foreach (TreeNode child in tn.Nodes)
                            {
                                if (child.Checked == true)
                                {
                                    MENUGROUP group1 = new MENUGROUP();
                                    group1.GROUPID = this.cmbgroup.SelectedValue.ToString();
                                    group1.MENUID = child.Name.ToString();
                                    dsmenu.Add(group1);
                                }
                            }
                        }
                    }
                    // thuc hien luu tru suong du lieu 
                    bool Iscomplete = true;
                    foreach (MENUGROUP mnu in dsmenu)
                    {
                        Iscomplete = MENUGROUPBUS.Insert(mnu);
                    }
                    if (Iscomplete)
                    {
                        frmHoidap f = new frmHoidap("Thêm Phân quyền thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                        f.ShowDialog();
                        SetSave();
                        UnCheckALL();
                        panel2.Enabled = false;
                    }
                    if(!Iscomplete)
                    {
                        frmHoidap f = new frmHoidap("Thêm Phân quyền thất bại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        f.ShowDialog();
                        SetSave();
                        panel2.Enabled = false;
                    }
                }
                
            }
            if (flag == 3)
            {
                if (this.cmbgroup.SelectedIndex != -1)
                {
                    frmHoidap f = new frmHoidap("Bạn có chắc muốn xóa phân quyền cho Group không ?!", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        bool IsComplete = true;
                        MENUGROUP mn = new MENUGROUP();
                        mn.GROUPID = this.cmbgroup.SelectedValue.ToString();
                        IsComplete = MENUGROUPBUS.Delete(mn);
                        if (IsComplete)
                        {
                            frmHoidap thanh = new frmHoidap("Xóa Phân quyền thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            thanh.ShowDialog();
                            SetSave();
                            UnCheckALL();
                            panel2.Enabled = false;
                        }
                        if (!IsComplete)
                        {
                            frmHoidap thanh = new frmHoidap("Xóa Phân quyền thất bại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            thanh.ShowDialog();
                            SetSave();
                            UnCheckALL();
                            panel2.Enabled = false;
                        }
                    }
                    else
                    {
                        SetSave();
                        UnCheckALL();
                        UnCheckALL();
                        panel2.Enabled = false;
                    }
                }
            }
            if (flag == 2)
            { 
                //duyet lai tat ca 
                //xoa tat ca cac muc khong duoc check trong danh muc neu ton tai o du lieu 
                if(this.cmbgroup.SelectedIndex!=-1)
                {
                    //duyet tat ca phan khong duoc check 
                    
                    MENUGROUPCollection dsMnuCheck = new MENUGROUPCollection();
                    
                    //thuc hien xoa het nhung menu moi bi bo check 
                   //thuc hien xoa het cua group do 
                    bool IsCompleteUnCheck = true;
                    MENUGROUP mn = new MENUGROUP();
                    mn.GROUPID = this.cmbgroup.SelectedValue.ToString();
                    IsCompleteUnCheck = MENUGROUPBUS.Delete(mn);
                    bool IscompleteCheck = true;
                    if(IsCompleteUnCheck)
                    {
                        //thuc hien comit danh sach

                        foreach (TreeNode tn in tvdanhsachtinhnang.Nodes)
                        {
                            //luu du lieu nodes cha
                            if (tn.Checked == true)
                            {
                                MENUGROUP group = new MENUGROUP();
                                group.GROUPID = this.cmbgroup.SelectedValue.ToString();
                                group.MENUID = tn.Name.ToString();
                                dsMnuCheck.Add(group);
                                foreach (TreeNode child in tn.Nodes)
                                {
                                    if (child.Checked == true)
                                    {
                                        MENUGROUP group1 = new MENUGROUP();
                                        group1.GROUPID = this.cmbgroup.SelectedValue.ToString();
                                        group1.MENUID = child.Name.ToString();
                                        dsMnuCheck.Add(group1);
                                    }
                                }
                            }
                        }
                        //thuc hien insert du lieu 
                        foreach (MENUGROUP mnu in dsMnuCheck)
                        {
                            IscompleteCheck = MENUGROUPBUS.Insert(mnu);
                        }
                    }
                    if (IscompleteCheck)
                    {
                        frmHoidap f = new frmHoidap("Cập nhật Phân quyền thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                        f.ShowDialog();
                        SetSave();
                        UnCheckALL();
                        panel2.Enabled = false;
                    }
                    if(!IscompleteCheck)
                    {
                        frmHoidap f = new frmHoidap("Cập nhật Phân quyền thất bại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        f.ShowDialog();
                        SetSave();
                        UnCheckALL();
                        panel2.Enabled = false;
                    }
                }

            }
        }

        private void toolbar1_Delete()
        {
            flag = 3;
            this.toolbar1.SetToolBar(false, false, false, true, true, false, true, false);
            panel2.Enabled = true;
            UnCheckALL();
        }

        private void toolbar1_Refresh()
        {
            flag = 0;
            UnCheckALL();
            panel2.Enabled = false;
            this.toolbar1.SetSave();
        }

        private void toolbar1_Edit()
        {
            flag = 2;
            this.toolbar1.SetToolBar(false, false, false, true, true, false, true, false);
            panel2.Enabled = true;
        }

        private void cmbgroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbgroup.SelectedIndex != -1)
            { 
                if(flag==2)
                {
                    // trang thai edit
                    //chon group muon chon 
                    UnCheckALL();
                    DataTable edit = new DataTable();
                    MENUGROUP mnu = new MENUGROUP();
                    mnu.GROUPID=this.cmbgroup.SelectedValue.ToString();
                    edit = MENUGROUPBUS.SelectPK(mnu);
                    if (edit.Rows.Count != 0)
                    {
                        //DUYET TAT CA FORM 
                        foreach (DataRow dr in edit.Rows)
                        { 
                            //kiem tra tat ca cac menu id 
                            foreach (TreeNode tn in tvdanhsachtinhnang.Nodes)
                            {
                                if (tn.Name == dr["MENUID"].ToString())
                                {
                                    tn.Checked = true;
                                }
                                foreach (TreeNode node in tn.Nodes)
                                {
                                    if (node.Name == dr["MENUID"].ToString())
                                    {
                                        node.Checked = true;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        frmHoidap thanh = new frmHoidap("Group này chưa được phân quyền vui lòng kiễm tra lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        thanh.ShowDialog();
                        SetSave();
                        UnCheckALL();
                        panel2.Enabled = false;
                    }
                }
            }
        }

        private void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }
    }
}
